<?php

namespace App\Providers;


use App\Model\Aboot\Wishlist;
use App\Model\Admin\Product;
use App\Model\Front\Category;
use App\Services\CategoriesChildService;
use Illuminate\Support\Facades\View;
use Gloudemans\Shoppingcart\Facades\Cart; 
use Illuminate\Support\ServiceProvider;

class ViewComposerServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        View::composer('front.layouts.header', function ($view) {
             $categorys = CategoriesChildService::frontTree(0);
             // dd($categorys);
             $products=Product::all();
             $products = json_encode($products);
            if (!is_file('product.json')) {
                file_put_contents('product.json', $products);
            }
            $carts = Cart::instance('shopping')->content();
            return $view->with('categorys', $categorys)->with('carts', $carts);
        });
        View::composer('front.layouts.check-login',function($view){
             $wishilist = Wishlist::where('user_id', auth()->id())->count();

            return $view->with('wcount', $wishilist);
        });

        View::composer('front.category.category', function ($view) {
            $categories = Category::all()->groupBy('parent_id');
            $categories['root'] = $categories[0];
            unset($categories[0]);
            return $view->with('categories', $categories);
        });
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}
